package proxy.cglib;

import java.lang.reflect.Method;

import lombok.AllArgsConstructor;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

@AllArgsConstructor
public class ProxyFactory implements MethodInterceptor {

	private Object target; // 被代理对象

	@Override
	public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		System.out.println("Cglib代理模式 ~~ 开始");
		Object returnVal = method.invoke(target, args);
		System.out.println("Cglib代理模式 ~~ 提交");
		return returnVal;
	}

	// 返回一个代理对象
	public Object getProxyInstance() {
		// 创建工具类
		Enhancer enhancer = new Enhancer();
		// 设置父类
		enhancer.setSuperclass(target.getClass());
		// 回调函数
		enhancer.setCallback(this);
		return enhancer.create();
	}

}
